package com.camel.studyApp;

import com.camel.studyApp.components.Component01;
import com.camel.studyApp.event.MyEvent;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Map;

/**
 * @author Administrator
 */
@SpringBootApplication
public class StudyAppApplication {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, IOException {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(StudyAppApplication.class, args);
        System.out.println(applicationContext);

        Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
        singletonObjects.setAccessible(true);
        ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
        Map<String, Object> map = (Map<String, Object>) singletonObjects.get(beanFactory);
        map.entrySet().stream().filter(e -> e.getKey().startsWith("component")).forEach(e -> {
            System.out.println(e.getKey() + " = " + e.getValue());
        });
        System.out.println("国际化:");
        System.out.println(applicationContext.getMessage("hi", null, Locale.CHINA));
        System.out.println(applicationContext.getMessage("hi", null, Locale.ENGLISH));
        System.out.println(applicationContext.getMessage("hi", null, Locale.JAPANESE));

        System.out.println("资源文件:");
        for (Resource resource : applicationContext.getResources("classpath:application.yml")) {
            System.out.println(resource);
        }

        System.out.println("通配符资源:");
        for (Resource resource : applicationContext.getResources("classpath*:META-INF/spring.factories")) {
            System.out.println(resource);
        }

        System.out.println("端口:");
        System.out.println(applicationContext.getEnvironment().getProperty("server.port"));


        System.out.println("事件");
        applicationContext.publishEvent(new MyEvent(new Component01()));

    }
}
